package hai.nguyenvan;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JSeparator;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTabbedPane;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.border.CompoundBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.ImageIcon;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private BufferedImage image;
	private ConnectionFrame connectionFrame;
	public static JCheckBoxMenuItem mnConnect;
	private JCheckBoxMenuItem mnExit;
	private JComboBox comboBox;

	public ConnectionFrame getConnectionFrame() {
		return connectionFrame;
	}

	public void setConnectionFrame(ConnectionFrame connectionFrame) {
		this.connectionFrame = connectionFrame;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 340, 376);
		setLocationRelativeTo(null);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnTools = new JMenu("Tools");
		menuBar.add(mnTools);

		mnConnect = new JCheckBoxMenuItem("Connect");
		mnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (connectionFrame == null) {
					connectionFrame = new ConnectionFrame(MainFrame.this);
					connectionFrame.setLocationRelativeTo(null);
				}
				connectionFrame.setVisible(true);
			}
		});
		mnTools.add(mnConnect);

		mnExit = new JCheckBoxMenuItem("Exit");
		mnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		mnTools.add(mnExit);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblChooseAttribute = new JLabel("Choose Attribute");
		lblChooseAttribute.setFont(new Font("Times New Roman", Font.BOLD, 20));
		lblChooseAttribute.setBounds(83, 0, 165, 50);
		contentPane.add(lblChooseAttribute);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 48, 304, 2);
		contentPane.add(separator);

		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TableFrame tableFrame = new TableFrame(comboBox
						.getSelectedItem().toString(), getConnectionFrame()
						.getConnection().getConn());
				tableFrame.setVisible(true);
			}
		});
		btnCreate.setBounds(120, 282, 89, 23);
		contentPane.add(btnCreate);

		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnClose.setBounds(225, 282, 89, 23);
		contentPane.add(btnClose);

		JLabel lblHaiNguyen = new JLabel("Hai Nguyen");
		lblHaiNguyen.setBounds(10, 284, 78, 19);
		contentPane.add(lblHaiNguyen);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(10, 279, 83, 2);
		contentPane.add(separator_1);

		JPanel panel = new JPanel();
		panel.setBounds(10, 61, 304, 210);
		contentPane.add(panel);
		panel.setLayout(null);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(
				new String[] { "List of tables" }));
		comboBox.setBounds(75, 11, 140, 20);
		panel.add(comboBox);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(35, 42, 235, 138);
		panel.add(lblNewLabel);

		try {
			image = ImageIO.read(MainFrame.class
					.getResource("/hai/nguyenvan/images/search.jpg"));
			setPicture(lblNewLabel, image);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void setPicture(JLabel label, BufferedImage image) {
		int x = label.getSize().width;
		int y = label.getSize().height;

		int ix = image.getWidth();
		int iy = image.getHeight();

		int dx = 0;
		int dy = 0;

		if (x / y > ix / iy) {
			dy = y;
			dx = dy * ix / iy;
		} else {
			dx = x;
			dy = dx * iy / ix;
		}

		ImageIcon icon = new ImageIcon(image.getScaledInstance(dx, dy,
				image.SCALE_SMOOTH));
		label.setIcon(icon);
	}

	public void loadTable() {
		// TODO Auto-generated method stub
		Connection conn = getConnectionFrame().getConnection().getConn();

		try {
			Statement stt = conn.createStatement();
			String query = "select table_name from user_tables";
			ResultSet rs = stt.executeQuery(query);

			DefaultComboBoxModel model = new DefaultComboBoxModel();

			while (rs.next()) {
				model.addElement(rs.getString(1));
			}

			comboBox.setModel(model);
			rs.close();
			stt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
